
if (typeof(__HELIPAD)!='boolean')
{
  var __HELIPAD=true;
  
  var __HELIPAD={
  
    __apiVersion : '1.0',
    __lastError : '',
    __helipadRoot : 'http://pad.helicoid.net',
    _helipadDefaultOffsetHoursDST : 1,
    _helipadDefaultOffsetMinutesDST : 0,
    _helipadDefaultOffsetHoursNonDST : 0,
    _helipadDefaultOffsetMinutesNonDST : 0,
    _helipadDefaultMaximumDifference : 10000,
    
    setRootUrl : function(rootUrl)
    {
      __HELIPAD.__helipadRoot=rootUrl;
    },
    
    checkCredentials : function(uid,pwd)
    {
      var RetVal=false;
      var svrResponse=null;
      
      __HELIPAD.__lastError='';
      
      if(__AJAX.GET(__HELIPAD.__helipadRoot+'/authenticate','email='+uid+'&password='+pwd,true))
      {
        svrResponse=__AJAX.getResponseText();
        if(svrResponse.indexOf('<authenticated>true</authenticated>')!=-1)
        {
          RetVal=true;
        }
      }
      else
        __HELIPAD.__lastError=__AJAX.getLastError();
      
      return(RetVal);
    },
    
    getDocTitles : function(uid,pwd)
    {
      var RetVal=null;
      var svrResponse=null;
      var requestDoc='<request><authentication><email>'+uid+'</email><password>'+pwd+'</password></authentication></request>';
      var parser = new DOMParser();
      var doc=null;
      var docs,xml;
      var counter;
      var serializer=new XMLSerializer();
            
      __HELIPAD.__lastError='';
      
      if(__AJAX.POST(__HELIPAD.__helipadRoot+'/documents/titles',requestDoc,true))
      {
        svrResponse=__AJAX.getResponseText();
        doc = parser.parseFromString(svrResponse,"text/xml");
        docs=doc.getElementsByTagName('document');
        if(docs.length>0)
        {
          RetVal=new Array();
          
          for(counter=0;counter<docs.length;counter++)
          {
            xml=serializer.serializeToString(docs[counter]);
            RetVal[counter]=new HelipadTitle(xml);
          }
        }
      }
      else
        __HELIPAD.__lastError=__AJAX.getLastError();
      
      return(RetVal);
    },
    
    getDocs : function(uid,pwd)
    {
      var RetVal=null;
      var svrResponse=null;
      var requestDoc='<request><authentication><email>'+uid+'</email><password>'+pwd+'</password></authentication></request>';
      var parser = new DOMParser();
      var doc=null;
      var docs,xml;
      var counter;
      var serializer=new XMLSerializer();
      
      __HELIPAD.__lastError='';
      
      if(__AJAX.POST(__HELIPAD.__helipadRoot+'/documents',requestDoc,true))
      {
        svrResponse=__AJAX.getResponseText();
        doc = parser.parseFromString(svrResponse,"text/xml");
        docs=doc.getElementsByTagName('document');
        if(docs.length>0)
        {
          RetVal=new Array();
          
          for(counter=0;counter<docs.length;counter++)
          {
            xml=serializer.serializeToString(docs[counter]);
            RetVal[counter]=new HelipadMemo(xml);
          }
        }
        else
          RetVal=new Array();
      }
      else
        __HELIPAD.__lastError=__AJAX.getLastError();
      
      return(RetVal);
    },

    deleteDoc : function(uid,pwd,id)
    {
      var RetVal=false;
      var svrResponse=null;
      var requestDoc='<request><authentication><email>'+uid+'</email><password>'+pwd+'</password></authentication></request>';
      
      __HELIPAD.__lastError='';
      
      if(__AJAX.POST(__HELIPAD.__helipadRoot+'/document/'+id+'/destroy',requestDoc,true))
      {
        svrResponse=__AJAX.getResponseText();
        if(svrResponse.indexOf('<deleted>true</deleted>')!=-1)
        {
          RetVal=true;
        }
      }
      else
        __HELIPAD.__lastError=__AJAX.getLastError();
      
      return(RetVal);
    },

    createDoc : function(uid,pwd,title,body,tnan)
    {
      var RetVal=false;
      var svrResponse=null;
      var docPart='<document><title>'+title+'</title><source>'+body+'</source><tags>\n__TNAN__:'+tnan+'\n</tags></document>';
      var requestDoc='<request><authentication><email>'+uid+'</email><password>'+pwd+'</password></authentication>'+docPart+'</request>';

      __HELIPAD.__lastError='';
      
      if(__AJAX.POST(__HELIPAD.__helipadRoot+'/document/create',requestDoc,true))
      {
        svrResponse=__AJAX.getResponseText();
        if(svrResponse.indexOf('<saved>true</saved>')!=-1)
        {
          RetVal=true;
        }
      }
      else
        __HELIPAD.__lastError=__AJAX.getLastError();
      
      return(RetVal);
    },

    updateDoc : function(uid,pwd,title,body,tnan,id)
    {
      var RetVal=false;
      var svrResponse=null;
      var docPart='<document><title>'+title+'</title><source>'+body+'</source><tags>\n__TNAN__:'+tnan+'\n</tags></document>';
      var requestDoc='<request><authentication><email>'+uid+'</email><password>'+pwd+'</password></authentication>'+docPart+'</request>';
      
      __HELIPAD.__lastError='';
      
      if(__AJAX.POST(__HELIPAD.__helipadRoot+'/document/'+id+'/update',requestDoc,true))
      {
        svrResponse=__AJAX.getResponseText();
        if(svrResponse.indexOf('<saved>true</saved>')!=-1)
        {
          RetVal=true;
        }
      }
      else
        __HELIPAD.__lastError=__AJAX.getLastError();
      
      return(RetVal);
    },

    updateTag : function(uid,pwd,tnan,id)
    {
      var RetVal=false;
      var svrResponse=null;
      var docPart='<document><tags>\n__TNAN__:'+tnan+'\n</tags></document>';
      var requestDoc='<request><authentication><email>'+uid+'</email><password>'+pwd+'</password></authentication>'+docPart+'</request>';
      
      __HELIPAD.__lastError='';
      
      if(__AJAX.POST(__HELIPAD.__helipadRoot+'/document/'+id+'/update',requestDoc,true))
      {
        svrResponse=__AJAX.getResponseText();
        if(svrResponse.indexOf('<saved>true</saved>')!=-1)
        {
          RetVal=true;
        }
      }
      else
        __HELIPAD.__lastError=__AJAX.getLastError();

      return(RetVal);
    },

    getLastError : function()
    {
      return(__HELIPAD.__lastError);
    },
    
    getApiVersion : function()
    {
      return(__HELIPAD.__apiVersion);
    }
    
  }
}